/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC15Fxx 系列 软件模拟串口举例----------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-755-82905966 -------------------------------------------*/
/* --- Tel: 86-755-82948412 -------------------------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了宏晶科技的资料及程序   */
/* 如果要在文章中应用此代码,请在文章中注明使用了宏晶科技的资料及程序   */
/*---------------------------------------------------------------------*/

//本示例在Keil开发环境下请选择Intel的8058芯片型号进行编译
//假定测试芯片的工作频率为18.432MHz

#include "uart.h"

BYTE TBUF,RBUF;
BYTE TDAT,RDAT;
BYTE TCNT,RCNT;
BYTE TBIT,RBIT;
BOOL TING,RING;
BOOL TEND,REND;

unsigned int for_int_i;
unsigned long int for_longint_i;

/*----------------------------
UART 初始设置
-----------------------------*/
void UartInit()
{
	TMOD &= 0xF0; // 高4位是设置的定时器1:不变，低4位设置定时器0为16位自动重装载模式
	AUXR |= 0x80; // |=0x80 定时器0为1T工作模式，
	//AUXR &= 0x7F; // &=0x7F 12T工作模式
    TL0 = BAUD;  // 16位初值的低8位
    TH0 = BAUD>>8; // 16位初值的高8位  16位自动重装载的值
    TR0 = 1;  // TCON控制位 定时器0 开始启用计数
    ET0 = 1; //打开定时器0中断
    PT0 = 1;                            //improve timer0 interrupt priority
    EA = 1; // 打开总中断
	
    TING = 0;
    RING = 0;
    TEND = 1;
    REND = 0;
    TCNT = 0;
    RCNT = 0;

    // SendString("uart begin!\r\n");
}
/*----------------------------
UART 中断服务程序
-----------------------------*/
void tm0() interrupt 1 using 1
{
    if (RING)
    {
        if (--RCNT == 0)
        {
            RCNT = 3;                   //reset send baudrate counter
            if (--RBIT == 0)
            {
                RBUF = RDAT;            //save the data to RBUF
                RING = 0;               //stop receive
                REND = 1;               //set receive completed flag
            }
            else
            {
                RDAT >>= 1;
                if (RXB) RDAT |= 0x80;  //shift RX data to RX buffer
            }
        }
    }
    else if (!RXB)
    {
        RING = 1;                       //set start receive flag
        RCNT = 4;                       //initial receive baudrate counter
        RBIT = 9;                       //initial receive bit number (8 data bits + 1 stop bit)
    }

		if (TING)                       //judge whether sending
    {
        if (1 || --TCNT == 0)
        {
            TCNT = 3;                       //reset send baudrate counter
            if (TBIT == 0)
            {
                TXB = 0;                //send start bit 开始位 引脚拉低
                TDAT = TBUF;            //load data from TBUF to TDAT
                TBIT = 9;               //initial send bit number (8 data bits + 1 stop bit)
            }
            else
            {
                TDAT >>= 1;             //shift data to CY
                if (--TBIT == 0)
                {
                    TXB = 1; // 结束位 引脚置高
                    TING = 0;           //stop send
                    TEND = 1;           //set send completed flag
                }
                else
                {
                    TXB = CY;           //write CY to TX port
                }
            }
        }
    }
}
/*----------------------------
发送串口数据
----------------------------*/
void SendData(BYTE dat)
{
    while (!TEND);
    TEND = 0;
    TBUF = dat;
    TING = 1;
    
    //return dat;
}

/*----------------------------
发送字符串
----------------------------*/
void SendString(unsigned char code *puts)
{
    for (; *puts != 0;	puts++)  SendData(*puts);
}
/*
发送长整型数字
*/
void SendLongInt(unsigned long int mylongint)
{
    // 四字节 10亿
	  for(for_longint_i=1000000000;for_longint_i>0;for_longint_i /= 10){
		    if((mylongint/for_longint_i)>0){
		        SendData( (mylongint/for_longint_i) + 48 );
			      mylongint = mylongint%for_longint_i;
        }else{
			      SendData('0');
        }
		}
}
/*
发送整型数字
*/
void SendInt(unsigned int myint)
{
    // 双字节 万位
	  for(for_int_i=10000;for_int_i>0;for_int_i /= 10){
		    if((myint/for_int_i)>0){
		        SendData( (myint/for_int_i) + 48 );
			      myint = myint%for_int_i;
        }else{
			      SendData('0');
        }
		}
}